package com.dhsc.service.impl.ywzj;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dhsc.domain.ywzx.ywzj.JzclthlVo;
import com.dhsc.domain.yypt.LayuiTableVo;
import com.dhsc.domain.yypt.ResponseObject;
import com.dhsc.exception.MyException;
import com.dhsc.local.dao.ywzj.IJzclthlDao;
import com.dhsc.service.ywzj.IJzclthlService;
import com.dhsc.util.CommonUtil;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;

import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName: JzclthlServiceImpl
 * @Description: 集中处理退回率
 * @date: 2023/9/11 11:31
 * @author: gx
 */
@Service
@Slf4j
public class JzclthlServiceImpl implements IJzclthlService {

    @Autowired
    private IJzclthlDao jzclthlDao;

    @Override
    public LayuiTableVo queryJzclthl(JzclthlVo jzclthlVo) {
        LayuiTableVo obj = new LayuiTableVo();
        try {
            PageMethod.startPage(jzclthlVo.getPage(), jzclthlVo.getLimit());
            List<JzclthlVo> list = jzclthlDao.queryJzclthl(jzclthlVo);
            PageInfo<JzclthlVo> pageInfo = new PageInfo<>(list);

            List<JzclthlVo> result = new ArrayList<>();
            if(CommonUtil.isNotEmptyList(list))
            {
            	for (int i = 0; i < list.size(); i++) {
                    //如果是第一个或部门档位与上一个不同时
                    if (i == 0 || (i > 0 && !list.get(i).getJjywdj().equals(list.get(i - 1).getJjywdj()))){
                        //添加一个部门档位名称到部门名称中
                        JzclthlVo jzclthl = new JzclthlVo();
                        jzclthl.setBmmc(list.get(i).getJjywdj());
                        jzclthl.setState(1);
                        result.add(jzclthl);
                    }
                    //正常将数据添加进去
                    result.add(list.get(i));
                }
            }
            obj.setData(result);
            obj.setMsg("");
            obj.setCount(pageInfo.getTotal());
            obj.setCode(0);
        }catch (Exception e){
            log.error("查询集中处理退回率信息出错",e);
            throw new MyException("查询集中处理退回率信息出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject queryRqnd() {
        ResponseObject obj = new ResponseObject();
        try {
            List<JzclthlVo> list = jzclthlDao.queryRqnd();
            obj.setData(list);
            obj.setStatus(200);
        }catch (Exception e){
            log.error("查询年份信息出错",e);
            throw new MyException("查询年份信息出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject queryRqjd() {
        ResponseObject obj = new ResponseObject();
        try {
            List<JzclthlVo> list = jzclthlDao.queryRqjd();
            obj.setData(list);
            obj.setStatus(200);
        }catch (Exception e){
            log.error("查询季度信息出错",e);
            throw new MyException("查询季度信息出错",e);
        }
        return obj;
    }
}
